<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class OrdersjifenController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
	public function index(){
		$shanchude=M('orders_jifen')->where(array('order_status'=>'-1'))->select();//查出为支付的订单
		foreach ($shanchude as $zk=>$zv){
			M('order_goods_jifen')->where(array('order_id'=>$zv['order_id']))->delete();
			M('orders_jifen')->where(array('order_id'=>$zv['order_id']))->delete();
		}
		$order_no = I('order_no')?I('order_no'):null;
		$type = I('type')?I('type'):null;
		if(!empty($order_no)){
			$where['order_no']=array('like','%'.$order_no.'%');
		}
		if(!empty($type)){
			$where['order_status']=$type;
		}else{
		    $where['order_status'] = array('neq','-9');
		}
		$count=M('Orders_jifen')->where($where)->order('add_time desc')->count();
		$Page=new Page($count,8);
		//setPageParameter($Page, array('account'=>$account));
		$show       = $Page->show();
		$list =  M('Orders_jifen')->where($where)
		->limit($Page->firstRow.','.$Page->listRows)->order('add_time desc')->select();
		foreach ($list as &$v){
			$nickname = M('Member')->where(array('openid'=>$v['open_id']))->find();
			$v['nickname'] = $nickname['nickname'];
			/*if($v['send_money'] == '9999.00'){
			    $v['send_money'] = "邮费到付";
			}
			if($v['send_money'] == '0.00'){
			    $v['send_money'] = "包邮";
			}*/
		}
		//dump($list);die;
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	public function detail(){
        $order_id = I('order_id');
        $is_tui  = I('is_tui');
        $list = M('Orders_jifen o')
                ->field('o.*,o.add_time as oadd_time,m.*')
                ->join('yang_member m ON m.openid=o.open_id')
                ->where('o.order_id ='.$order_id)
                ->find();
        $goods = M('Order_goods_jifen og')
                ->field("og.*,ga.attr_val,g.*")
                ->join('yang_goods g ON og.goods_id=g.goods_id')
                ->join('yang_goods_attr ga ON og.attr_id=ga.goods_attr_id')
                ->where('og.order_id ='.$order_id)
                ->select();
        if($is_tui){
            $list = M('Orders_jifen o')
            ->field('o.*,o.add_time as oadd_time,m.*,ot.*')
            ->join('yang_member m ON m.openid=o.open_id')
            ->join('yang_order_tui ot ON ot.order_id=o.order_id')
            ->where(array('o.order_id'=>$order_id,'order_status' =>4))
            ->find();
            //dump($list);die;
            $goods = M('Order_goods_jifen og')
            ->field("og.*,ga.attr_val,g.*")
            ->join('yang_goods g ON og.goods_id=g.goods_id')
            ->join('yang_goods_attr ga ON og.attr_id=ga.goods_attr_id')
            /* ->join('yang_order_tui ot ON ot.order_id=o.order_id,ot.goods_id=og.goods_id') */
            ->where(array('og.order_id'=>$order_id))
            ->select();
            /*foreach ($goods as &$v){
                $tui = M('order_tui')->where(array('order_id'=>$v['order_id'],'goods_id'=>$v['goods_id'],'attr_name'=>$v['attr_name']))->find();
                if($tui){
    				$v['tui'] =$tui['status'];
    			}else{
    				$v['tui'] = -99;
    			}
    			if($v['tui'] == 0){
    			    $v['tui'] = '已发起申请';
    			}
    			if($v['tui'] == 1){
    			    $v['tui'] = '同意申请退款';
    			}
    			if($v['tui'] == -1){
    			    $v['tui'] = '拒绝申请退款';
    			}
    			if($v['tui'] == -99){
    			    $v['tui'] = '尚未发起申请';
    			}
            }*/
            /*$this->assign('tui',1);*/
        }
       /* if($list['send_money'] >0){
            $list['send_money'] = "邮费到付";
        }
        if($list['send_money'] == '0.00'){
            $list['send_money'] = "包邮";
        }*/
        $this->assign('list',$list);
        $this->assign('goods',$goods);
        $this->display();
	}
	public function setStatus(){
		$order_id = I('order_id');
		$kuaidi = I('kuaidi');
		$is_tui = I('is_tui');
		$kuaidigongsi=I("kuaidigongsi");
		$re = M('Orders_jifen')->where('order_id ='.$order_id)->find();
		
	
		    if($re['order_status'] == 2 && $re['kuaidi'] == $kuaidi){
		        $this->error('已经是发货状态了！');
		    }else{ 
		        if($re['order_status'] == 1 && $kuaidi == ''){
		            $this->error("请选择是否发货并填写快递单号");
		        }
		        if($re['order_status'] == 1 && $kuaidigongsi == ''){
		            $this->error("请选择是否发货并填写快递公司");
		        
		        }
		    }
		/*dump($_POST);
		dump($re);
		die;*/
		
		$time = time();
		$res  = M('Orders_jifen')->where('order_id ='.$order_id)->save(array('order_status'=>2,'kuaidigongsi'=>$kuaidigongsi,'kuaidi'=>$kuaidi,'send_time'=>$time));
		if($res){
		    $this->error('操作成功',U('Admin/Ordersjifen/index'));
		}else{
		    $this->error('操作失败');
		}
	}
	
	
}